<?php

$server = new swoole_websocket_server("0.0.0.0", 9502);
$server->on('open', function (swoole_websocket_server $server, $request) {
});

$server->on('message', function (swoole_websocket_server $server, $frame) {
    print_r($server->connections);
    if (json_decode($frame->data)->type == "login") {
        $GLOBALS['fd'][$frame->fd]['uid'] = json_decode($frame->data)->data;
    }
    if (json_decode($frame->data)->array) {//私聊
        foreach (json_decode($frame->data)->array as $key => $fd) {
            $user_message = $frame->data;
            $server->push($fd, $user_message);
        }
    } else {//群发信息
        foreach ($server->connections as $key => $fd) {
            $user_message = $frame->data;
            $server->push($fd, $user_message);
        }
    }

});

$server->on('close', function ($server, $fd) {
    foreach ($server->connections as $k => $v) {
        $server->push($v, json_encode(array('type' => 'out', 'uid' => $GLOBALS['fd'][$fd]['uid'])));
    }
});

$server->start();
